/**
 * 音频控制器
 */
define(['eventEmitter','./Util'],function(EventEmitter,util){
	var Audio = function(){
		//常用标识
		this.audioEle = false;
		/**
		 * 初始化方法
		 * @param {String} audioId 音频标签Id
		 * @param {String} rsUrl 资源URL
		 * @param {Function} callback 当文件就绪可以开始播放时运行的脚本（缓冲已足够开始时）
		 */
		this.init = function(audioId,rsUrl,callback){
			//设置URL
			this.__initAudio(audioId);
			this.__setResource(rsUrl);
				console.log('音频初始化');
				callback();
		}
		
		/**
		 * 播放的方法
		 */
		this.play = function(){
			this.audioEle.play();
		}
		
		/**
		 * 暂停的方法
		 */
		this.pause = function(){
			this.audioEle.pause();
		}
		
		/**
		 * 是否正在播放
		 */
		this.isPlaying = function(){
			return !this.audioEle.paused;
		}
		
		/**
		 * 得到缓冲百分比
		 */
		this.getBufferedPercent = function(){
			return ( this.audioEle.buffered.end( 0 ) / this.audioEle.duration ) * 100;
		}
		
		/**
		 * 得到音频总时长
		 */
		this.getAudioDuration = function(){
			return this.audioEle.duration;
		}
		
		
		/**
		 * 得到当前播放时间,单位 秒
		 */
		this.getCurrentTime = function(){
			return this.audioEle.currentTime;
		}
		
		/**
		 * 得到当前播放时间百分比
		 */
		this.getCurrentTimePercent = function(){
			return ( this.audioEle.currentTime / this.audioEle.duration ) * 100;
		}
		
		/**
		 * 设置音量大小
		 * @param {Float} volume 音量大小 0~1
		 */
		this.setVolume = function(volume){
			this.audioEle.volume = volume;
		}
		
		/**
		 * 得到音量大小
		 */
		this.getVolume = function(){
			return this.audioEle.volume;
		}
		
		/**
		 * 设置播放时间
		 * @param {Int} time 设置的时间
		 */
		this.setCurrentTime = function(time){
			this.audioEle.currentTime = time;
		}
		
		/**
		 * 显示正在加载中
		 */
		this.loading = function(){
		};
		
		
		//私有方法
		/**
		 * 初始化audio
		 * @param {String} audioId audio标签Id
		 */
		this.__initAudio = function(audioId){
			this.audioEle = document.getElementById(audioId);
			//oncanplay事件
			this.audioEle.oncanplay = function(){
				this.emit('canplay');
			}.bind(this);
			//onplay事件
			this.audioEle.onplay = function(){
				this.emit('play');
			}.bind(this);
			//onpause事件
			this.audioEle.onpause = function(){
				this.emit('pause');
			}.bind(this);
			//seeked事件
			this.audioEle.onseeked = function(){
				this.emit('seeked');
			}.bind(this);
		}
		/**
		 * 设置资源
		 * @param {String} rsUrl 资源URL
		 */
		this.__setResource = function(rsUrl){
			while (this.audioEle.firstChild) {
			    this.audioEle.removeChild(this.audioEle.firstChild);
			}
			
			var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
            var isFF = userAgent.indexOf("Firefox") > -1 ; //判断是否Firefox
            var isChrome = userAgent.indexOf("Chrome") > -1 ; //判断是否Chrome
            if (isChrome==false && isFF==false) {
            	this.audioEle.src = rsUrl + '/audio.aac';
            }else{
            	//更改为通过source加载
            	var x = document.createElement("SOURCE");
            	x.setAttribute("src", rsUrl + '/audio.ogg');
            	this.audioEle.appendChild(x);

           		var y = document.createElement("SOURCE");
            	y.setAttribute("src", rsUrl + '/audio.mp3');
            	this.audioEle.appendChild(y);

           		var z = document.createElement("SOURCE");
            	z.setAttribute("src", rsUrl + '/audio.aac');
            	this.audioEle.appendChild(z);
			}
		}
	};
	
	//继承事件
	util.inherits(Audio,EventEmitter);
	return Audio;
});